package com.zlfund.cat.zjgsmk.net;

import android.content.ComponentName;
import android.content.Intent;
import android.text.TextUtils;

import com.zlfund.cat.zjgsmk.bean.BaseBean;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;

/**
 * Created by chenj
 * Created on 2017/8/22
 */

public class RequestCall<T extends BaseBean,S> {

    private Retrofit mRetrofit;
    private Class<?>[] mClazzes;
    private Object[] mArgs;
    private Class<S> mServiceClass;
    private Call<T> mCall;
    private Observable<T> mObservable;

    RequestCall(Class<S> clazz, Object[] args, Class<?>[] clazzes){
        mClazzes = clazzes;
        mServiceClass = clazz;
        mArgs = args;
    }

    public void cancel(){
        if (mObservable == null){
            mCall.cancel();
        }else {
            mObservable.unsubscribeOn(Schedulers.io());
        }
    }

    public Observable<T> enqueueWithRx(String methodName, final INetCallback<T> callback){
        mRetrofit = RetrofitFactory.createGsonAndRxRetrofit();
        S s = mRetrofit.create(mServiceClass);
        try {
            Method method;
            if (mClazzes == null){
                method = mServiceClass.getMethod(methodName);
            }else {
                method = mServiceClass.getMethod(methodName, mClazzes);
            }
            mObservable = (Observable<T>) method.invoke(s, mArgs);
            mObservable.observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Consumer<T>() {
                        @Override
                        public void accept(T t) throws Exception {
                            String e = t.getE();
                            if (!TextUtils.isEmpty(e)){
                                callback.error(t.getE());
                                return;
                            }
                            callback.response(t);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            callback.error("系统繁忙，请稍后再试");
                        }
                    });

            return mObservable;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Call<T> enqueue(Callback<T> callback,String methodName){
        mRetrofit = RetrofitFactory.createGsonRetrofit();
        S s = mRetrofit.create(mServiceClass);
        try {
            Method m = mServiceClass.getMethod(methodName,mClazzes);
            mCall = (Call<T>) m.invoke(s,mArgs);
            mCall.enqueue(callback);
            return mCall;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

}
